package com.plian.system.config;

import com.plian.system.listener.TaskCompletedListener;
import com.plian.system.listener.TaskDeletedListener;
import org.flowable.common.engine.api.delegate.event.FlowableEventListener;
import org.flowable.spring.SpringProcessEngineConfiguration;
import org.flowable.spring.boot.EngineConfigurationConfigurer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.*;

/**
 * 工作流监听器配置类
 */
@Configuration
public class FlowableListenerConfig {
    //工作流监听器的类型
    private static final String FLOWABLE_LISTENER_TYPE_TASK_COMPLETED = "TASK_COMPLETED";

    private static final String FLOWABLE_LISTENER_TYPE_ENTITY_DELETED = "ENTITY_DELETED";

    private final TaskCompletedListener taskCompletedListener;

    private final TaskDeletedListener taskDeletedListener;

    @Autowired
    public FlowableListenerConfig(TaskCompletedListener taskCompletedListener,TaskDeletedListener taskDeletedListener) {
        this.taskCompletedListener = taskCompletedListener;
        this.taskDeletedListener=taskDeletedListener;
    }

    /**
     * 将自定义监听器纳入flowable监听
     */
    @Bean
    public EngineConfigurationConfigurer<SpringProcessEngineConfiguration> globalListenerConfigurer() {
        return engineConfiguration -> engineConfiguration.setTypedEventListeners(this.customFlowableListeners());
    }

    /**
     * 自定义监听类配置
     */
    private Map<String, List<FlowableEventListener>> customFlowableListeners() {
        Map<String, List<FlowableEventListener>> listenerMap = new HashMap<>();
        //listenerMap.put(FLOWABLE_LISTENER_TYPE_TASK_COMPLETED, new ArrayList<>(Collections.singletonList(getTaskCompletedListener())));
        listenerMap.put(FLOWABLE_LISTENER_TYPE_ENTITY_DELETED, new ArrayList<>(Collections.singletonList(getTaskDeletedListener())));
        return listenerMap;
    }

    /**
     * 获取任务生成监听器
     */
    public TaskCompletedListener getTaskCompletedListener() {
        return taskCompletedListener;
    }

    public TaskDeletedListener getTaskDeletedListener() {
        return taskDeletedListener;
    }
}
